package uk.co.cocking.getinline2.builder;

import java.io.File;

import org.hamcrest.Matcher;

import uk.co.cocking.getinline2.pipeline.io.FileLineReader;
import uk.co.cocking.getinline2.pipeline.io.FileLineWriter;
import uk.co.cocking.getinline2.pipeline.io.RssConverter;
import uk.co.cocking.getinline2.pipeline.io.StringLineReader;
import uk.co.cocking.getinline2.pipeline.io.StringLineWriter;
import uk.co.cocking.getinline2.pipeline.io.UrlRetriever;
import uk.co.cocking.getinline2.pipeline.io.XsltTransformer;
import uk.co.cocking.getinline2.pipeline.transformers.Filter;
import uk.co.cocking.getinline2.pipeline.transformers.StringWrapper;
import uk.co.cocking.getinline2.pipeline.transformers.Transformer;
import uk.co.cocking.getinline2.record.Record;
import uk.co.cocking.getinline2.restlet.StringRestRequester;

@SuppressWarnings("unchecked")
public class StringConsumerBuilder<OriginalType, IncomingType, OutgoingType> extends Builder<OriginalType, IncomingType, OutgoingType> {

	public StringConsumerBuilder(
			Transformer<IncomingType, OutgoingType> transformer,
			Builder<OriginalType, ?, IncomingType> previousBuilder) {
		super(transformer, previousBuilder);
	}
	
	public StringConsumerBuilder() {
		super();
	}

	public StringConsumerBuilder<OriginalType, String, String> stringLineReader() {
		return createStringConsumer(new StringLineReader());
	}
	
	public StringConsumerBuilder<OriginalType, String, String> fileReader() {
		return createStringConsumer(new FileLineReader());
	}
	
	public StringConsumerBuilder<OriginalType, String, String> stringFilter(Matcher<? super String> matcher) {
		 return createStringConsumer(new Filter<String>(matcher));
	}
	
	public StringConsumerBuilder<OriginalType, String, String> writeToFile(String fileName) {
		return createStringConsumer(new FileLineWriter(fileName));
	}
	
	public StringConsumerBuilder<OriginalType, String, String> transformUsingXslt(String stylesheetName) {
		return createStringConsumer(new XsltTransformer(new File(stylesheetName)));
	}
	
	public StringConsumerBuilder<OriginalType, String, String> stringLineWriter(StringLineWriter transformer) {
		return createStringConsumer(transformer);
	}
	
	public StringConsumerBuilder<OriginalType, String, String> retrieveUrls() {
		return createStringConsumer(new UrlRetriever());
	}
	
	public RecordConsumerBuilder<OriginalType, String, Record> stringWrapper(String fieldName) {
		return createRecordConsumer(new StringWrapper(fieldName));
	}
	
	public RecordConsumerBuilder<OriginalType, String, Record> readRssItems() {
		return createRecordConsumer(new RssConverter());
	}
	
	public StringConsumerBuilder<OriginalType, String, String> post(String baseUrl) {
		return createStringConsumer(new StringRestRequester(baseUrl));
	}
	
	public StringConsumerBuilder<OriginalType, String, String> fileWriter(String fileName) {
		return createStringConsumer(new FileLineWriter(fileName));
	}

	private StringConsumerBuilder<OriginalType, String, String> createStringConsumer(Transformer<String, String> transformer) {
		return new StringConsumerBuilder<OriginalType, String, String>(transformer, (Builder<OriginalType, String, String>) this);
	}
	
	private RecordConsumerBuilder<OriginalType, String, Record> createRecordConsumer(Transformer<String, Record> transformer) {
		return new RecordConsumerBuilder<OriginalType, String, Record>(transformer, (Builder<OriginalType, String, String>) this);
	}

}
